package view.servlet.category;

import com.alibaba.fastjson.JSON;
import dao.CategoryDao;
import dao.impl.CategoryDaoImpl;
import pojo.Category;
import pojo.User;
import pojo.vo.Message;
import service.CategoryService;
import service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addCategory")
public class AddCategory extends HttpServlet {
    CategoryDao categoryDao = new CategoryDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        User user = (User) req.getSession().getAttribute("user");
        int uid = user.getId();
        String data = req.getParameter("data");
        String[] t1 = data.split("-");
        String ctg1 = t1[0];
        String ctg2 = t1[1];
        if(categoryDao.queryFirstCode(uid, ctg1)!=0){
            int pcode = categoryDao.queryFirstCode(uid,ctg1);
            categoryDao.insertSecondCategory(uid,pcode,ctg2);
        }else{
            categoryDao.insertFirstCategory(uid,ctg1);
            int pcode = categoryDao.queryFirstCode(uid,ctg1);
            categoryDao.insertSecondCategory(uid,pcode,ctg2);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
